/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package buzzerproxy;

import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Enger
 */
public class XMLParser {

    public final String CLIENT_ID = "clientid";
    public final String CLIENT_VERSION = "clientversion";
    public final String SEARCH_SERVICE = "searchservice";
    public final String FILE_TYP = "filetyp";
    public final String RESULT_PAGE = "page";
    public final String SEARCH_QUERY = "searchquery";

    public final HashMap<String, String> parseResults(String rawResults) {

        if ( rawResults == null || !(rawResults instanceof String)|| rawResults.isEmpty() ){
            Exception e = new Exception();
            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, e);
            System.exit(1);
        } // if ( rawResults == null || !(rawResults instanceof String)){
            
        HashMap<String, String> results = new HashMap<String, String>();
        String[] arr = rawResults.split("<");
        String key = null;
        String value = null;
                
        for (int i = 0; i < arr.length; i++) {
            //System.out.println("array"+i+"  :"+arr[i]);
            try {
                String[] splitter = arr[i].split(">");
                key = splitter[0];
                value = splitter[1];
                if ((key != null && !key.isEmpty()) && ( value != null && !value.isEmpty() ) ){
                    
                    splitter[0].trim();
                    splitter[1].trim();
                    splitter[0] = splitter[0].replaceAll("\\s+", "");	 // Achtung, hier wird der Whitespace entfernt	   
                    splitter[1] = splitter[1].replaceAll("\\s+", "");
                    
                    results.put(key, value);
                    
                } // if ((key != null && !key.isEmpty()) && ( value != null && !value.isEmpty() ) ){
            } catch (ArrayIndexOutOfBoundsException a) {
                 Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, a);
            } catch ( NullPointerException n){
                Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, n);
            }

        } // for (int i = 0; i < arr.length; i++) {
        
        return results;
        
    } // public final HashMap<String, String> parseResults(String rawResults) {

//////////    public static void main(String[] args)
//////////    {
//////////
//////////        String file = new String("J:\\Java Development\\Buzzer\\SuchprotokollBuzzer.xml");
//////////        String fileContent = "";
//////////        try {
//////////            fileContent = Gzip.readFileAsString(file);
//////////        } catch (IOException ex) {
//////////            Logger.getLogger(GoogleResultParser.class.getName()).log(Level.SEVERE, null, ex);
//////////        }
//////////       
//////////        HashMap results = parseResults(fileContent) ;
//////////        String s2 = (String) results.get(CLIENT_ID);
//////////        System.out.println(s2);
//////////        
//////////    }
    
} // public class XMLParser {